package com.cloudcross.ssp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.adinall.dsp.core.utils.DateUtils;
import com.adinall.dsp.service.AccountService;
import com.cloudcross.ssp.common.utils.Common;
import com.cloudcross.ssp.model.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.model.Log;
import com.cloudcross.ssp.service.ILogService;
import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.common.utils.SqlHelper;

import javax.servlet.http.HttpServletRequest;

@Service
public class LogService implements ILogService {
    @Autowired
    private GenericIBatisDao myBatisDao;
    @Autowired
    private AccountService accountService;

    @Override
    public Log findById(Long id) {
        return myBatisDao.get("logSqlMapper.selectById", id);
    }

    @Override
    public int countByParams(Map<String, Object> paramMap, String module) {
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("key", paramMap.get("searchKey"));
        paramMap.put("value", searchValue);
        paramMap.put("module", module);
        return myBatisDao.get("logSqlMapper.countByParams", paramMap);
    }

    @Override
    public List<Log> findByParams(Map<String, Object> paramMap, Pager pager, String module) {
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("key", paramMap.get("searchKey"));
        paramMap.put("value", searchValue);
        paramMap.put("module", module);
        paramMap.put("pager", pager);
        return myBatisDao.getList("logSqlMapper.selectByParams", paramMap);
    }

    @Override
    public List<Log> findByParams1(HashMap<String, Object> paramMap) {
        return myBatisDao.getList("logSqlMapper.selectByParams1", paramMap);
    }

    @Override
    public boolean add(Log t) {
        t.setAccountId(t.getAccountId());
        t.setOperTime(new Date());
        myBatisDao.save("logSqlMapper.addLog", t);
        return true;
    }

    @Override
    public boolean edit(Log t) {
        t.setOperTime(new Date());
        myBatisDao.save("logSqlMapper.editLog", t);
        return true;
    }

    // 涉及到信息修改记录的没用到这个方法
    @Override
    public boolean updateStatus(List<Long> idList, int status) {
        return false;
    }

    // 涉及到信息修改记录的没用到这两个方法
    @Override
    public int countByParams(Map<String, Object> paramMap) {
        return 0;
    }

    @Override
    public List<Log> findByParams(Map<String, Object> paramMap, Pager pager) {
        return null;
    }

    @Override
    public List<Log> findDeliverEffectAnalysis(Map<String, Object> paramMap) {
        return myBatisDao.getList("logSqlMapper.findDeliverEffectAnalysis", paramMap);
    }

    @Override
    public int countByParamsBack(Map<String, Object> paramMap) {
        return myBatisDao.get("logSqlMapper.countByParamsBack", paramMap);
    }

    @Override
    public List<Log> findByParamsBack(Map<String, Object> paramMap, Pager pager) {
        paramMap.put("pager", pager);
        return myBatisDao.getList("logSqlMapper.findByParamsBack", paramMap);
    }

    @Override
    public List<Log> findByParamsBack(Map<String, Object> paramMap) {
        return myBatisDao.getList("logSqlMapper.findByParams1Back", paramMap);
    }

    @Override
    public Integer findLogCountByCondition(Map<String, Object> condition) {
        return myBatisDao.get("logSqlMapper.selectLogCountByCondition", condition);
    }

    @Override
    public List<Log> findLogByCondition(Map<String, Object> condition, Pager pager) {
        if (pager == null) {
            return myBatisDao.getList("logSqlMapper.selectLogExportByCondition", condition);
        } else {
            condition.put("pager", pager);
            return myBatisDao.getList("logSqlMapper.selectLogByCondition", condition);
        }
    }

    @Override
   public  void saveLogAction(HttpServletRequest request, String module, String tableName, Long recordId, String action, String actionType){
        Account account = accountService.getSessionUser();
        Log log = new Log();
        log.setAccountId(account.getId());
        log.setAccountName(account.getRoleName());
        log.setUserIp(Common.toIpAddr(request));
        log.setActionTime("100");
        log.setActionTime("100");
        log.setAction(action);
        log.setRecordId(recordId);
        log.setOperTime(DateUtils.now());
        log.setTableName(tableName);
        log.setAction(action);
        log.setActionType(actionType);
        log.setModule(module);
        myBatisDao.save("logSqlMapper.addLog", log);
   }
}
